package com.dandelion.admin.dao;

import com.dandelion.admin.criteria.OrderListQueryCriteria;
import com.dandelion.common.domain.OrderStatusEnum;
import com.rockit.core.pojo.Pagination;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

public class OrderDaoSqlProvider {
    public String queryOrderItemSql(@Param("orderNo") String orderNo) {
        SQL sql = new SQL();
        sql.SELECT("t.id as id");
        sql.SELECT("t.order_no as orderNo");
        sql.SELECT("t.img as img");
        sql.SELECT("IFNULL(t1.name, '') as subCatName");
        sql.SELECT("IFNULL(t2.name, '') as leafCatName");
        sql.SELECT("t.item_num as itemNum");
        sql.SELECT("t.model as model");
        sql.SELECT("t.req as req");
        sql.SELECT("t.weight as weight");
        sql.SELECT("t.width as width");
        sql.SELECT("t.length as length");
        sql.SELECT("t.height as height");

        sql.FROM("t_order_item t");
        sql.LEFT_OUTER_JOIN("t_category_tree t1 on t.sub_cat_id=t1.id");
        sql.LEFT_OUTER_JOIN("t_category_tree t2 on t.leaf_cat_id=t2.id");
        sql.WHERE("t.order_no=#{orderNo} and t.is_deleted=0");
        sql.ORDER_BY("t.id asc");
        return sql.toString();
    }

    public String querySql(@Param("c") OrderListQueryCriteria criteria, Pagination pagination) {
        SQL sql = new SQL();
        sql.SELECT("t.id AS id");
        sql.SELECT("t.order_no AS orderNo");
        sql.SELECT("t1.name as topCatName");
        sql.SELECT("t3.name AS svrName");

        sql.SELECT("t.order_cct_name AS orderCctName");
        sql.SELECT("t.order_cct_mobile as orderCctMobile");

        sql.SELECT("t.cst_name AS cstName");
        sql.SELECT("t.cst_mobile as cstMobile");

        sql.SELECT("IFNULL(cst_attr_p.name,'') as cstAddrProv");
        sql.SELECT("IFNULL(cst_attr_c.name,'') as cstAddrCity");
        sql.SELECT("IFNULL(cst_attr_a.name,'') as cstAddrArea");
        sql.SELECT("IFNULL(cst_attr_d.name,'') as cstAddrDist");
        sql.SELECT("t.cst_addr_detail as cstAddrDetail");
        sql.SELECT("t.bld_lift as bldLift");
        sql.SELECT("t.bld_floor as bldFloor");
        sql.SELECT("t.verify as verify");
        sql.SELECT("t.verify_code as verifyCode");

        sql.SELECT("t2.name as exprComName");
        sql.SELECT("t.expr_status as exprStatus");
        sql.SELECT("t.expr_status as exprStatusInt");
        sql.SELECT("t.expr_order_no as exprOrderNo");
        sql.SELECT("t.cst_expect_deliver_date as cstExpectDeliverDate");
        sql.SELECT("t.est_deliver_date as estDeliverDate");

        sql.SELECT("t.pk_addr as pkAddr");

        sql.SELECT("t.pk_cct_name as pkCctName");
        sql.SELECT("t.pk_cct_mobile as pkCctMobile");
        sql.SELECT("t.pkg_num as pkgNum");
        sql.SELECT("t.pre_fr_pay as preFrePay");
        sql.SELECT("t.`status` as `status`");
        sql.SELECT("t.`special_req` as `specialReq`");
        sql.SELECT("t.`create_time` as `createTime`");
        sql.SELECT("t.`update_time` as `updateTime`");
        sql.FROM("t_order t");
        sql.LEFT_OUTER_JOIN("t_category_tree t1 on t.top_cat_id=t1.id");
        sql.LEFT_OUTER_JOIN("t_expr_company t2 on t.expr_com_id=t2.id");
        sql.LEFT_OUTER_JOIN("t_custom_service t3 on t.svr_id=t3.id");

        sql.LEFT_OUTER_JOIN("t_address_tree cst_attr_p on t.cst_addr_prov_code=cst_attr_p.code");
        sql.LEFT_OUTER_JOIN("t_address_tree cst_attr_c on t.cst_addr_city_code=cst_attr_c.code");
        sql.LEFT_OUTER_JOIN("t_address_tree cst_attr_a on t.cst_addr_area_code=cst_attr_a.code");
        sql.LEFT_OUTER_JOIN("t_address_tree cst_attr_d on t.cst_addr_dist_code=cst_attr_d.code");

        sql.ORDER_BY("t.create_time DESC");

        //where
        whereClause(sql, criteria);

        //order by
        sql.ORDER_BY("t.create_time desc, t.id asc");

        //limit
        StringBuilder buf = new StringBuilder(512);
        sql.usingAppender(buf);
        buf.append(" limit ").append(pagination.getOffset()).append(",").append(pagination.getLimit());

        return buf.toString();
    }

    public String countSql(@Param("c") OrderListQueryCriteria criteria) {
        SQL sql = new SQL();
        sql.SELECT("count(1)");
        sql.FROM("t_order t");
        whereClause(sql, criteria);
        return sql.toString();
    }

    private void whereClause(SQL sql, OrderListQueryCriteria criteria) {
        sql.WHERE("t.is_deleted=0");
        if (StringUtils.isNotBlank(criteria.getOrderNo())) {
            sql.AND().WHERE("t.order_no like concat('%',#{c.orderNo}, '%')");
        }
        if (StringUtils.isNotBlank(criteria.getExprOrderNo())) {
            sql.AND().WHERE("t.expr_order_no like concat('%',#{c.exprOrderNo}, '%')");
        }
        if (StringUtils.isNotBlank(criteria.getCstName())) {
            sql.AND().WHERE("t.cst_name like concat('%',#{c.cstName}, '%')");
        }
        if (StringUtils.isNotBlank(criteria.getCstMobile())) {
            sql.AND().WHERE("t.cst_mobile like concat('%',#{c.cstMobile}, '%')");
        }
        if (StringUtils.isNotBlank(criteria.getOrderCctName())) {
            sql.AND().WHERE("t.order_cct_name like concat('%',#{c.orderCctName}, '%')");
        }
        if (StringUtils.isNotBlank(criteria.getOrderCctMobile())) {
            sql.AND().WHERE("t.order_cct_mobile like concat('%',#{c.orderCctMobile}, '%')");
        }
        if (StringUtils.isNotBlank(criteria.getPkCctName())) {
            sql.AND().WHERE("t.pk_cct_name like concat('%',#{c.pkCctName}, '%')");
        }
        if (StringUtils.isNotBlank(criteria.getPkCctMobile())) {
            sql.AND().WHERE("t.pk_cct_mobile like concat('%',#{c.pk_cct_mobile}, '%')");
        }

        if (StringUtils.isNotBlank(criteria.getExprStatus())) {
            sql.AND().WHERE("t.expr_status=#{c.exprStatus}");
        }

        if (criteria.getOrderStatus() == OrderStatusEnum.FINISHED) {
            sql.AND().WHERE("t.status=1");
        } else if (criteria.getOrderStatus() == OrderStatusEnum.CANCELLED) {
            sql.AND().WHERE("t.status=2");
        } else if (criteria.getOrderStatus() == OrderStatusEnum.PROCESSING) {
            sql.AND().WHERE("t.status=0");
        }
    }
}
